package cn.edu.zut.hhr.service;

import cn.edu.zut.hhr.entity.SecurityUser;
import cn.edu.zut.hhr.entity.User;
import cn.edu.zut.hhr.enums.IddentityEnum;
import cn.edu.zut.hhr.utils.GatewayUtil;
import cn.edu.zut.hhr.utils.RestResponse;
import io.micrometer.core.ipc.http.HttpSender;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.web.accept.ParameterContentNegotiationStrategy;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
public class TokenUserDetailsService implements UserDetailsService {
    //请求网关来获取对象

    @Resource
    private RestTemplate restTemplate;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        ParameterizedTypeReference<RestResponse<User>> typeRef = new ParameterizedTypeReference<RestResponse<User>>() {};
        ResponseEntity<RestResponse<User>> re = restTemplate.exchange(GatewayUtil.PATH + "/user/" + s, HttpMethod.GET,null,typeRef);
        User user = re.getBody().getData();
        return new SecurityUser(user);
    }
}
